﻿using DeviceMonitor.Dao;
using DeviceMonitor.Model;
using DeviceMonitor.ViewModel;
using DeviceMonitor.Windows;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//using System.Data.SQLite;

namespace DeviceMonitor.Pages
{
    /// <summary>
    /// BillPage.xaml 的交互逻辑
    /// </summary>
    public partial class BillPage : Page
    {
        BillViewModel viewModel;
        public BillPage()
        {
            InitializeComponent();

            viewModel = new BillViewModel();

            viewModel.queryParam.timeBeggin = DateTime.Now.ToString("yyyy-MM-dd");
            viewModel.queryParam.timeEnd = DateTime.Now.ToString("yyyy-MM-dd");

            this.DataContext = viewModel;

            this.Loaded += Page_loaded;
        }

        private void Page_loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                loadData();
            }catch (Exception ex)
            {
                MessageBox.Show("加载数据错误");
            }
        }
        private void loadData()
        {
            try
            {
                Hashtable paraMap = new Hashtable();
                paraMap.Add("queryStartDate", viewModel.queryParam.timeBeggin);
                paraMap.Add("queryEndDate", viewModel.queryParam.timeEnd);

                ArrayList list = (ArrayList)SqlMapDao.instance().QueryForList("selectBills", paraMap);
                viewModel.list.Clear();
                foreach (BillModel bill in list)
                {
                    viewModel.list.Add(bill);
                }
            }
            catch (Exception ex)
            {
                
            }
        }

        private void Button_Click_Query(object sender, RoutedEventArgs e)
        {
            loadData();
        }

        private void showBillDetail(object sender, RoutedEventArgs e)
        {
            BillModel selectedRow = (BillModel)dataGrid.SelectedItem;

            BillItemsWindow detailWindow = new BillItemsWindow();
            detailWindow.setBillID(selectedRow.id);
            bool result = (bool)detailWindow.ShowDialog();
        }

        private void Button_Click_Test(object sender, RoutedEventArgs e)
        {
            /*
            SQLiteConnection m_dbConnection;
            m_dbConnection = new SQLiteConnection("Data Source=D:\\sqliteDBs\\QRSHGEM2.db;Version=3;");
            m_dbConnection.Open();
            string sql = "select trucksno,productsnamebak,cjz,cmzsj,orderno,customername from saleorders ";
            sql += " where orderdate >= '2021-04-01 00:00:00' ";
            sql += " and isactive = '1' and cmzsj is not null order by cmzsj";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read())
                Console.WriteLine("trucksno: " + reader[0] + "productsname: " + reader[1]);

            reader.Close();
            m_dbConnection.Close();
            */
            //MainWindow.DoUploadBillSqlite();

            Hashtable paraMap = new Hashtable();
            paraMap.Add("queryStartDateTime", "2021-01-01 00:00:00");
            paraMap.Add("queryEndDateTime", "2021-09-01 00:00:00");

            ArrayList list = (ArrayList)MsSqlMapDao.instance().QueryForList("selectBills", paraMap);
            Console.WriteLine(list.Count + "");
        }
    }
}
